
#ifndef COMMON_TASK_PROCESSOR_H_
#   error Do not include "task-processor-aux.h" directly, include "task-processor.h" instead
#endif

#ifndef COMMON_TASK_PROCESSOR_AUX_H_
#define COMMON_TASK_PROCESSOR_AUX_H_

namespace fasmio { namespace common {

template <typename HostClass, typename TaskClass>
TaskProcessor<HostClass, TaskClass>::TaskProcessor(ILogger* logger, const char* fiber_name) :
    FiberPool(logger, fiber_name),
    queue_(nullptr),
    host_(nullptr),
    handler_(nullptr)
{
}

template <typename HostClass, typename TaskClass>
TaskProcessor<HostClass, TaskClass>::~TaskProcessor()
{
}

template <typename HostClass, typename TaskClass>
bool TaskProcessor<HostClass, TaskClass>::Start(unsigned int fiber_count, queue_t &queue, HostClass *host, task_handler handler)
{
    queue_ = &queue;
    if (nullptr == (host_ = host))
        return false;
    if (nullptr == (handler_ = handler))
        return false;

    return FiberPool::Start(fiber_count);
}

template <typename HostClass, typename TaskClass>
void TaskProcessor<HostClass, TaskClass>::Stop()
{
    FiberPool::Stop();
}

template <typename HostClass, typename TaskClass>
void TaskProcessor<HostClass, TaskClass>::Worker()
{
    while (!quit_event_->IsSet())
    {
        TaskClass *task = queue_->Pop(true, 10);
        if (nullptr == task)
            continue;

        (host_->*handler_)(task);
        queue_->TaskDone();
    }
}

}}  // namespace fasmio::common

#endif  // COMMON_TASK_PROCESSOR_AUX_H_

